package server.business.http;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpResponse;

import java.util.Map;

import server.business.entity.ResultMessage;
import server.business.service.HttpServiceFactory;
import server.business.service.IHttpService;

import com.alibaba.fastjson.JSON;
import com.core.rest.AbstractHttpHandler;

public class RechargeHandler extends AbstractHttpHandler implements Cloneable {

    @Override
    protected Object clone() throws CloneNotSupportedException {

        return super.clone();
    }

    @Override
    public AbstractHttpHandler deepCopy() {

        AbstractHttpHandler httpHandler = null;

        try {

            httpHandler = (AbstractHttpHandler)(this.clone());

        } catch (CloneNotSupportedException e) {

            e.printStackTrace();
        }

        return httpHandler;
    }

    @Override
    protected void getData(Map<String, String> params, ChannelHandlerContext ctx) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void postData(Map<String, String> params, ChannelHandlerContext ctx) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void deleteData(Map<String, String> params, ChannelHandlerContext ctx) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void updateData(Map<String, String> params, ChannelHandlerContext ctx) {
        String text = "";

        IHttpService service = HttpServiceFactory.getService();

        ResultMessage rm = new ResultMessage(true);

        if (params == null || params.isEmpty()) {

            rm.setSuccess(false);

        } else {

            String userId = params.get("userId");

            String currency = params.get("currency");

            long coins = Long.parseLong(params.get("coins"));

            long recharge = service.recharge(userId, currency, coins);

            rm.setMessage(recharge);

            //text = JSON.toJSONString(list, true);

        }

        text = JSON.toJSONString(rm, true);

        FullHttpResponse response = wrapContent(text);

        sendResponse(ctx, response);

    }

}
